﻿using CommonMethods.Extend;
using MahApps.Metro.Controls;
using MaterialDesignThemes.Wpf;
using MiniBlink.Core;
using System;
using System.Windows;
using System.Windows.Controls;

namespace MiniBLinkSample
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : MetroWindow
    {
        public MainWindow()
        {
            InitializeComponent();

            /*获取版本号*/
            ProgramVersion.Text = $"Version:{System.Windows.Application.ResourceAssembly.GetName().Version.ToString()}";

            #region 绑定浏览器事件

            BlinkBrowserViewer.OnUrlChangedEvent += BlinkBrowser_OnUrlChangedEvent;
            BlinkBrowserViewer.OnTitleChangedEvent += BlinkBrowser_OnTitleChanged;
            BlinkBrowserViewer.OnDocumentReadyEvent += BlinkBrowser_OnDocumentReadyEvent;
            BlinkBrowserViewer.OnDownloadFile += BlinkBrowser1_OnDownloadFile;
            BlinkBrowserViewer.OnUrlNavigationEvent += BlinkBrowserViewer_OnUrlNavigationEvent;
            BlinkBrowserViewer.OnMouseOverUrlChangedEvent += BlinkBrowser_OnMouseOverUrlChangedEvent;
            #endregion
        }

        private void BlinkBrowserViewer_OnUrlNavigationEvent(object sender, WebNavigateEventArgs e)
        {
            ShowProgressInfo(true);
        }

        /// <summary>
        /// URL 地址改变事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BlinkBrowser_OnUrlChangedEvent(object sender, UrlChangedEventArgs e)
        {
            WebUrlInput.Text = e.Url;
        }

        /// <summary>
        /// 鼠标滑过a标签，返回链接url
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="url"></param>
        private void BlinkBrowser_OnMouseOverUrlChangedEvent(object sender, string url)
        {
            WebTipsText.Text = url;
        }


        private void BlinkBrowser1_OnDownloadFile(string url)
        {

        }

        /// <summary>
        /// 网页标题改变
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="title"></param>
        private void BlinkBrowser_OnTitleChanged(object sender, string title)
        {
            this.Title = title;
        }

        /// <summary>
        /// 对应js里的body onload事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BlinkBrowser_OnDocumentReadyEvent(object sender, EventArgs e)
        {
            BlinkBrowserViewer.Focus();     /*定位焦点到浏览器上*/
            ShowProgressInfo();
        }
        

        private void LaunchMahAppsOnGitHub(object sender, RoutedEventArgs e)
        {
            System.Diagnostics.Process.Start("https://gitee.com/rgq0503/MiniBlinkPinvoke.git");
        }

        private void MetroWindow_Loaded(object sender, RoutedEventArgs e)
        {
        }

        /// <summary>
        /// 打开网址
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BtnOpenWebUrl_Click(object sender, RoutedEventArgs e)
        {
            OpenWebUrl(WebUrlInput.Text.Trim());
        }

        /// <summary>
        /// 打开网址
        /// </summary>
        /// <param name="url"></param>
        private void OpenWebUrl(string url)
        {
            if (url.IsEmpty())
            {
                MainSnackbar.MessageQueue.Enqueue("请求的网址链接为空！");
            }
            else
            {
                BlinkBrowserViewer.Url = url;
            }
        }

        /// <summary>
        /// 是否显示进度条
        /// </summary>
        /// <param name="flag"></param>
        private void ShowProgressInfo(bool flag = false)
        {
            if (flag)
            {
                LoadingWebViewProgress.Visibility = Visibility.Visible;
                LoadingWebViewProgress.IsIndeterminate = true;
            }
            else
            {
                LoadingWebViewProgress.Visibility = Visibility.Collapsed;
                LoadingWebViewProgress.IsIndeterminate = false;
            }
        }

        private void WebUrlInput_PreviewKeyDown(object sender, System.Windows.Input.KeyEventArgs e)
        {
            if(e.KeyStates == System.Windows.Input.KeyStates.Down && e.Key == System.Windows.Input.Key.Enter)
            {
                OpenWebUrl(WebUrlInput.Text.Trim());
            }
        }

        private void BtnToolsBar_Click(object sender, RoutedEventArgs e)
        {
            Button btn = sender as Button;
            switch (btn.Tag)
            {
                case "BackOff":
                    break;
                case "Forward":
                    break;
                case "Refresh":
                    break;
                case "Home":
                    break;
                case "BookMark":
                    break;
            }
        }

        //private void MainSnackbar_IsActiveChanged(object sender, RoutedPropertyChangedEventArgs<bool> e)
        //{
        //    if (e.NewValue)
        //    {
        //        MainSnackbarHost.Width = 320;
        //        MainSnackbarHost.Height = 46;
        //    }
        //    else
        //    {
        //        MainSnackbarHost.Width = 0;
        //        MainSnackbarHost.Height = 0;
        //    }
        //}
    }
}
